<?php

	session_start();
	
	// check for login
	if(!isset($_SESSION['atbat']) && !isset($_COOKIE['remember_atbat'])) {
	
		// no login, move to index page
		header("Location: http://localhost/atbat/html/");
	
	}
	
	// connect to db
	require_once '../db/db.php';
	
	// get user id
	$sql = "SELECT user_id FROM users WHERE MD5(username) IN ('";
	$sql .= ((isset($_SESSION['atbat'])) ? $_SESSION['atbat'] : '') . "', '";
	$sql .= ((isset($_COOKIE['remember_atbat'])) ? $_COOKIE['remember_atbat'] : '') . "')";
		
	$id = $db->query($sql)->fetchAll();
	$user_id = $id[0]['user_id'];

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <base href="http://localhost/atbat/html/" />
	<meta charset="utf-8">
	<title>AtBat :: Games</title>
	<meta name="description" content="">
	<meta name="author" content="Sean Mullin, http://parametercontraption.com">
	<!--  Mobile Viewport Fix j.mp/mobileviewport & davidbcalhoun.com/2010/viewport-metatag -->
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
	<link rel="shortcut icon" href="images/design/favicon.ico">
	<link rel="apple-touch-icon" href="images/design/apple-touch-icon.png">
	<link rel="stylesheet" href="css/screen.css">
	<!-- For the less-enabled mobile browsers like Opera Mini --><link rel="stylesheet" media="all" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/base/jquery-ui.css" />
    <script src="js/modernizr-1.5.min.js"></script>
</head>
<body>
	<?php require_once 'include/header.html'; ?>
    <section id="content">
    	<h1>Games</h1>
        <article>
        	<h2>Start a New Game</h2>
            <p><a href="new-game">Score a new game</a>.</p>
            <table id="games" cellpadding="0" cellspacing="0" summary="All the user's current games">
            	<caption>Your Games</caption>
                <tr>
                	<th>Status</th>
                    <th>Date</th>
                    <th>Box Score</th>
                    <th>R</th>
                    <th>H</th>
                    <th>E</th>
                </tr>
                <?php
				
					// loop thru this users games
					$sql = "SELECT games.game_id, games.status, games.date, t1.city, t1.name, t2.city, t2.name, IF(game_details.inning_half = 0, 'Top', 'Bottom'), game_details.inning_number, game_details.away_runs, game_details.away_hits, game_details.away_errors, game_details.home_runs, game_details.home_hits, game_details.home_errors FROM games LEFT JOIN teams AS t1 ON t1.team_id = games.away_team LEFT JOIN teams AS t2 ON t2.team_id = games.home_team LEFT JOIN game_details ON game_details.game_id = games.game_id WHERE games.user_id = " . $user_id;
										
					foreach($db->query($sql) as $game) {
				
				?>
                <tr class="g_<?php echo $game[0]; ?>">
                	<td rowspan="2">
						<?php
                        
							// show status of game
							// 0 = in progress
							// 1 = complete
							if($game[1] == 0) {
								
								echo '<a href="edit-game/' . $game[0] . '">In Progress</a><br/>';	
								// little trick to show ordinal of inning number
								echo $game[7] . ' of the ' . date('jS', strtotime($game[8]));
								
							}else{
								
								echo '<a href="view-game/' . $game[0] . '">Complete</a>';
								
							}
						
						?>
					</td>
                    <td><?php echo date('m-d-Y', strtotime($game[2])); ?></td>
                    <td><?php echo $game[3] . ' ' . $game[4]; ?></td>
                    <td><?php echo $game[9]; ?></td>
                    <td><?php echo $game[10]; ?></td>
                    <td><?php echo $game[11]; ?></td>
                </tr>
                <tr class="g_<?php echo $game[0]; ?>">
                	<td><a href="delete-game/<?php echo $game[0]; ?>" class="deleteGame">Delete Game</a></td>
                    <td><?php echo $game[5] . ' ' . $game[6]; ?></td>
                    <td><?php echo $game[12]; ?></td>
                    <td><?php echo $game[13]; ?></td>
                    <td><?php echo $game[14]; ?></td>
                </tr>
                <?php } ?>
            </table>
        </article>
    </section>
    <?php require_once 'include/footer.html'; ?>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>
    <script>
    	$(document).ready(function() {
			// show delete confirm on link click
			var g_id = "";
			$(".deleteGame").click(function() {
				var game_id = $(this).attr("href").split("/");
				g_id = game_id[1];
				
				$("#dialogDelete").dialog("open");
				
				return false;
			});
			
			// delete dialog
			$("#dialogDelete").dialog({
				autoOpen: false,
				modal: true,
				draggable: false,
				resizable: false,
				buttons: {
					"Delete Game": function() {
						$.post(
							"delete-game.php",
							{
								id: g_id,
								delete_game: true
							},
							function(data) {
								$(".error").remove();
								$(".success").remove();
								$(".notice").remove();
								$("#dialogDelete").prepend(data);
								if($(data).hasClass('success')) {
									// remove game from table
									$(".g_" + g_id).fadeOut();
									
									// close window after 2.5 seconds on success
									setTimeout(function() { $("#dialogDelete").dialog("close"); }, 2500);
								}
							}
						);
					},
					"Cancel": function() { $(this).dialog("close"); }
				}
			});
		});
    </script>
    <div id="dialogDelete" title="Confirm Deletion">
    	<p>Do you really want to delete this game?</p>
    </div>
</body>
</html>